﻿Imports System.Windows.Forms

Public Class frmSettings

    Private Function checkValues() As Boolean

        If String.IsNullOrEmpty(Me.txtBOCPass.Text.Trim) OrElse _
                   String.IsNullOrEmpty(Me.txtBOCUname.Text.Trim) OrElse _
                   String.IsNullOrEmpty(Me.txtGPass.Text.Trim) OrElse _
                   String.IsNullOrEmpty(Me.txtSmtpServer.Text.Trim) OrElse _
                   String.IsNullOrEmpty(Me.txtSmtpPort.Text.Trim) OrElse _
                    String.IsNullOrEmpty(Me.txtEmail1.Text.Trim) OrElse _
                   String.IsNullOrEmpty(Me.txtGUname.Text.Trim) Then

            MsgBox("All fields except email2 are required", MsgBoxStyle.Critical)
            Return False

        End If

        If IsNumeric(Me.txtSmtpPort.Text.Trim) = False Then
            MsgBox("SMTP Port must be numeric", MsgBoxStyle.Critical)
            Return False
        End If

        Return True

    End Function
    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click

        If Not checkValues() Then
            Return
        End If

        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click

        If Not checkValues() Then
            Return
        End If

        Dim boc As New BocDownloader

        boc.BocPassword = Me.txtGPass.Text
        boc.BocUsername = Me.txtBOCUname.Text
        boc.SmtpPass = Me.txtGPass.Text
        boc.SmtpUname = Me.txtGUname.Text
        boc.Email2 = Me.txtEmail2.Text
        boc.Email1 = Me.txtEmail1.Text
        boc.SmtpServer = Me.txtSmtpServer.Text
        boc.SmtpPort = CInt(Me.txtSmtpPort.Text)

        boc.runDownload()

    End Sub

    Private Sub frmSettings_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Me.txtBOCPass.Text = Encrypt.Decrypt(GetSetting("BOCDN", "Main", "BOCPass", ""))
        Me.txtBOCUname.Text = Encrypt.Decrypt(GetSetting("BOCDN", "Main", "BOCUname", ""))
        Me.txtGPass.Text = Encrypt.Decrypt(GetSetting("BOCDN", "Main", "GPass", ""))
        Me.txtGUname.Text = Encrypt.Decrypt(GetSetting("BOCDN", "Main", "GUname", ""))
        Me.txtEmail1.Text = GetSetting("BOCDN", "Main", "Email1", "")
        Me.txtEmail2.Text = GetSetting("BOCDN", "Main", "Email2", "")
        Me.txtSmtpServer.Text = GetSetting("BOCDN", "Main", "SmtpServer", "")
        Me.txtSmtpPort.Text = GetSetting("BOCDN", "Main", "SmtpPort", "")
    End Sub
End Class
